package com.xapp.activity.mainActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;

import com.xapp.baseContent.SplashActivity;
import com.xapp.utils.ConstantUils;
import com.xapp.utils.PermissionUtils;
import com.xapp.yhjsBirdge.CallBackFunction;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;

/**
 * @author YH_CK
 **/
public class MyMainHandler extends Handler {

    private WeakReference<MainActivity> mActivity;
    private PermissionUtils mPermissionUtils;
    private SetPopListener listener;

    MyMainHandler(MainActivity activity, SetPopListener listener) {
        this.mActivity = new WeakReference<>(activity);
        this.mPermissionUtils = new PermissionUtils(activity, this);
        this.listener = listener;
    }

    @Override
    public void handleMessage(Message msg) {
        MainActivity activity = mActivity.get();
        if (mActivity == null) {
            super.handleMessage(msg);
            return;
        }
        switch (msg.what) {
            case 1001://callback data
                CallBackFunction mCallBackFunction = activity.mRegisterFucUtils.getCallBackFunction(msg.getData().getString("key"));
                Log.d("HTTP-CallBack-" + msg.getData().getString("key"), msg.getData().getString("jsonData"));
                if (mCallBackFunction != null) {
                    mCallBackFunction.onCallBack(msg.getData().getString("jsonData"));
                }
                break;
            case 10002:// 打开浏览器
                String url = msg.getData().getString("data");
                activity.openLocalBrowser(url);
                break;
            case 10003://打开 UVCCAMERA
                activity.startActivityForResult(new Intent(activity, com.xapp.XthermDemo.MainActivity.class), ConstantUils.OPEN_UVCCAMERA);
                break;
            case 10004:// 获取经纬度
                // GPS获取
                 activity.getGps();
                // 百度获取
//                 activity.getGpsBd();
//                activity.getLocation();
                break;
            case 1006://check  Location Permission
                activity.mMainPresenterCompl.checkPermissionHandler(ConstantUils.ACCESS_COARSE_LOCATION_REQUEST_CODE, 1002, Manifest.permission.ACCESS_COARSE_LOCATION);
                break;
            case 1007: //check  Contact Permission
                activity.mMainPresenterCompl.checkPermissionHandler(ConstantUils.OPEN_CONTACTS_REQUEST_CODE, 1003, Manifest.permission.READ_CONTACTS);
                break;
            case 1009://close app
                activity.closeActivity(msg.getData().getString("data"));
                break;
            case 1010://check Permission
                mPermissionUtils.needPermission(msg.getData().getString("permission"), msg.getData().getInt("requestCode"), msg.getData().getInt("messageCode"));
                break;
            case 1011://check  Contact Permission
                activity.mMainPresenterCompl.checkPermissionHandler(ConstantUils.READ_CONTACTS_REQUEST_CODE, 1004, Manifest.permission.READ_CONTACTS);
                break;
            case 1012://open system settings
                activity.startActivity(new Intent(Settings.ACTION_SETTINGS));
                break;
            case 1013://check  SMS Permission
                activity.mMainPresenterCompl.checkPermissionHandler(ConstantUils.GET_SMS_REQUEST_CODE, 1014, Manifest.permission.READ_SMS);
                break;
            case 1014://get sms
                activity.mMainPresenterCompl.getSmsInfo();
                break;
            case 1015://check netWork
                activity.mMainPresenterCompl.checkNetWorkAvailable(msg.getData().getString("data"));
                break;
            case 1016://check exetrnalStorage Permission
                activity.mImagePickerJson = msg.getData().getString("data");
                try {
                    JSONObject imageJson = new JSONObject(activity.mImagePickerJson);
                    int messageCode = 1017;
                    if (imageJson.has("type")) {
                        String type = imageJson.getString("type");
                        if ("1".equals(type)) {
                            messageCode = 1023;
                        } else if ("3".equals(type)) {
                            listener.click();
                            return;
                        }
                    }
                    activity.mMainPresenterCompl.checkPermissionHandler(ConstantUils.GET_EXTERNAL_STORAGE_REQUEST_CODE, messageCode, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case 1017://open system album
                activity.mMainPresenterCompl.openSystemPhotoAlbum();
                break;
            case 1018: //update image
                activity.mMainPresenterCompl.mUpdateImage(msg.getData().getString("data"));
            case 1020://show error view
                break;
            case 1021:// call phone
                activity.mPhone = msg.getData().getString("data");
                activity.mMainPresenterCompl.callPhone(activity.mPhone);
                break;
            case 1023://chcek camera permission
                activity.mMainPresenterCompl.checkPermissionHandler(ConstantUils.GET_CAMERA_REQUEST_CODE, 1024, Manifest.permission.CAMERA);
                break;
            case 1024:
                activity.mMainPresenterCompl.openSystemCamera();
                break;
            case 1026://check ocrID exetrnalStorage Permission
                activity.mMainPresenterCompl.checkPermissionHandler(ConstantUils.GET_EXTERNAL_STORAGE_REQUEST_CODE_OCRID, 1027, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                break;
            case 1028://chcek ocrbank camera permission
                activity.mMainPresenterCompl.checkPermissionHandler(ConstantUils.GET_CAMERA_REQUEST_CODE_OCRBANK, 1029, Manifest.permission.CAMERA);
                break;
            case 1029://check ocrbank exetrnalStorage Permission
                activity.mMainPresenterCompl.checkPermissionHandler(ConstantUils.GET_EXTERNAL_STORAGE_REQUEST_CODE_OCRBANK, 1030, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                break;
            case 1037://网络请求
                activity.mMainPresenterCompl.httpPostGetData(msg.getData().getString("data"));
                break;
            case 1042://检查相机权限
                activity.mMainPresenterCompl.checkPermissionHandler(ConstantUils.GET_CAMERA_LINKN_FACE, 1040, Manifest.permission.CAMERA);
                break;
            case 1043://状态栏颜色
                String jsonData = msg.getData().getString("data");
                try {
                    JSONObject jb = new JSONObject(jsonData);
                    String style = jb.optString("style");
                    mActivity.get().setStatusIconAndTextColor(style);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }

    interface SetPopListener {
        void click();
    }
}
